var form = layui.form,code=getQueryVariable("code"),id=0;

//表单初始值，修改时调用
/*form.val("character-form",{
    "code":"",
    "name":""
})
$("input[name='type']")[0].checked=true;*/
if(code!=""){
    $("span")[0].innerHTML="修改角色";
   var type= query(code);
    $("input[name='code']")[0].readOnly=true;
    setchecked(type,"type");
    form.render();
}else{
    $("span")[0].innerHTML="新增角色";
}

//监听提交
form.on('submit(character-form-element)', function(data){
    //data.field 为表单修改或新增数据 变量ID有值的情况下是修改，否则为新增
    if(id!=0){//修改
        var num =update(data.field);
        if(num < 10000 && num >0){
            layer.msg("保存成功", {icon: 6});
            refresh();
        }else if(num < 10000 && num ==0){
            layer.msg("本地保存失败", {icon: 6});
        }else if(num > 10000  ){
            layer.msg("同步云枢保存失败", {icon: 6});
        }
    }else{//新增
        var types=$("input[name='type']");
        var type=types[0].checked==true?1:2;
        data.field.type=type;
        if(isExit(data.field.name)){
            layer.msg("名称已存在!", {icon: 6});
            return false;
        }
        var num=insert(data.field);
        if(num < 10000 & num >0){
            layer.msg("保存成功", {icon: 6});
            refresh();
        }else if (num > 10000 ){
            layer.msg("同步云枢保存失败", {icon: 6});
        }else if(num <10000 && num ==0){
            layer.msg("本地保存失败", {icon: 6});
        }
    }
    return false;
});
function  refresh(){
    setTimeout(function () {
        //保存成功则刷新提交过来的界面 并关闭此页面
        parent.detab("views/jsp/SystemCenter/character.jsp");
        parent.intab("views/jsp/SystemCenter/character.jsp","角色设置")
        // parent.detab("views/jsp/SystemCenter/tpl/newupcharacter.jsp"+(id==''?'':'?code='+));
        parent.detab("views/jsp/SystemCenter/tpl/newupcharacter.jsp?code="+code);
        parent.detab("views/jsp/SystemCenter/tpl/newupcharacter.jsp");
    },1000)
}
function setchecked(data,name) {
    var type = $("input[name='" + name + "']");
    if (data == 1) {
        type[0].checked = true;
        type[0].value = 1;
        type[1].checked = false;
        type[1].value = 2;
    } else {
        type[0].checked = false;
        type[0].value = 1;
        type[1].checked = true;
        type[1].value = 2;
    }
}
function  query(code) {
    var type=1;
    $.ajax({
        type : 'GET',
        //  url : 'resource/getTree',
        data:{"code":code},
        url:'role/selectByCode',
        dataType:"json",
        /*  contentType: 'application/json',*/
        async:false,
        success : function (da) {
            if(da.status==200 ){
                var data=da.data;
                form.val("character-form",{
                    "code":data.code,
                    "name":data.name,
                    "type":data.type
                })
               type=data.type;
                id=data.id;
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return type;
}
function insert(data) {
    var flag=false;
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:JSON.stringify(data),
        url:'role/insert',
        dataType:"json",
          contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
               //flag=true;
                flag=da.data;
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return flag;
}
function  update(data) {
    data.id=id;
    var flag=false;
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:JSON.stringify(data),
        url:'role/update',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
               // flag=true;
                flag=da.data;
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return flag;
}
function isExit(name) {
var tf=false;
    $.ajax({
        type : 'GET',
        //  url : 'resource/getTree',
        data:{"name":name},
        url:'role/queryByName',
        dataType:"json",
        /*  contentType: 'application/json',*/
        async:false,
        success : function (da) {
            if(da.status==200 ){
               tf=da.data;
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return tf;
}